package DataStructure.iterator;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class IteratorMapTest {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("a", "1");
        map.put("b", "2");
        map.put("c", "3");

        //方式一：通过keySet方法获取所有的key
        Set<String> keySet = map.keySet();
        Iterator<String> iterator = keySet.iterator();
        while(iterator.hasNext()){
            String key = iterator.next();
            System.out.println(key+":"+map.get(key));
        }

        //方式二：通过Map.entry方法获取所有的Map.Entry对象
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for(Iterator<Map.Entry<String,String>> iterator1 = entrySet.iterator();iterator1.hasNext();){
            Map.Entry<String, String> entry = iterator1.next();
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
    }
}
